<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Ttc\Model;
use Think\Model;

/**
 * 用户模型
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */

class ContactModel extends Model {

    protected $_validate = array(
        // array('goods_storage','is_emptyStorage','请输入正确的库存',1,'function'),//库存不为0
        array('contact_name','','请输入联系人名称',1,'notequal',3),//商品名称不为空
        array('contact_phone','','请输入联系电话',1,'notequal',3),//商品名称不为空
        array('areaid_1','','请选择一级地区',1,'notequal',3),//一级地区不为空
        array('areaid_2','','请选择二级地区',1,'notequal',3),//二级地区不为空
        array('areaid_3','','请选择三级地区',1,'notequal',3),//三级地区不为空
        array('detailed_address','','请输入详细地址',1,'notequal',3),//详细地址不为空

    );

    /**
     * 添加土特产联系人
     * @param  integer $data 土特产信息数组
     */
    public function addContact($data){

        $res = $this->create($data,3);
        if(!$res){
            // var_dump($this->getError());
            // return $this->getError();
            $res = array();
            $res['info'] = $this->getError();
            $res['msg'] = false;
            return $res;
        }
        // var_dump($this->data);
        $res = $this->add();
        if(empty($res)){
            return false;
        }
        return $res;

    }
     /**
     * 修改土特产联系人
     * @param  integer $condition 条件
     * @param  integer $data 土特产信息数组
     */
    public function saveContact($condition,$data){

        $res = $this->create($data,3);
        if(!$res){
            // var_dump($this->getError());
            // return $this->getError();
            $res = array();
            $res['info'] = $this->getError();
            $res['msg'] = false;
            return $res;
        }
        // var_dump($this->data);
        $res = $this->where($condition)->save($data);
        return $res;

    }
    /**
     * 修改联系人默认状态
     * @param  integer $condition 条件
     * @param  integer $data 土特产信息数组
     */
    public function saveDefalut($condition,$data){
        $res = $this->where($condition)->save($data);
        return $res;

    }
     /**
     * 删除单条土特产联系人
     * @param  integer $id 地址id
     */
    public function deleteContact($id){
        $res = $this->delete($id);
        return $res;

    }
     /**
     * 查询联系信息
     * @param  integer $data 联系信息数组
     */
    public function ContactInfo($data){

        // var_dump($data);exit;
        $wh['goods_id'] = $data['goods_id'];
        $wh['member_id'] = $data['member_id'];

        $res = $this->field('contact_phone,contact_wechat')->where($wh)->find();
        // var_dump($res);exit;
        return $res;

    }
    /**
     * 查询单条联系信息
     * @param  integer $condition 查询条件数组
     * @param  integer $field 查询字段
     */
    public function getOneContact($condition,$field='*',$order='is_default desc,up_time desc,id desc'){
        $res = $this->where($condition)->order($order)->field($field)->find();
        // var_dump($res);exit;
        return $res;

    }
     /**
     * 查询多条联系信息(无分页)
     * @param  integer $condition 查询条件数组
     * @param  integer $field 查询字段
     */
    public function getAllContact($condition,$field='*',$order='up_time desc,id desc'){
        $res = $this->field($field)->where($condition)->order($order)->select();
        // var_dump($res);exit;
        return $res;

    }
    /**
     * 查询总条数
     */
    public function getNum($condition){
        //总条数
        $num = $this->field('count(*)')
        ->where($condition)->find();
        // var_dump($num['count(*)']);exit;
        return $num['count(*)'];

    }
    /**
     * 查询多条联系信息(分页)
     * @param  integer $condition 查询条件数组
     * @param  integer $field 查询字段
     */
    public function getAllContactPage($condition,$field='*',$order='is_default desc,up_time desc,id desc',$page,$rows){
        $start = ($page-1)*$rows;
        $res = $this->field($field)->where($condition)->limit($start.','.$rows)->order($order)->select();
        // var_dump($res);exit;
        return $res;

    }
    

}
